package com.xfc.web.controller.school;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xfc.common.annotation.Log;
import com.xfc.common.core.controller.BaseController;
import com.xfc.common.core.domain.AjaxResult;
import com.xfc.common.core.page.TableDataInfo;
import com.xfc.common.enums.BusinessType;
import com.xfc.common.utils.poi.ExcelUtil;
import com.xfc.school.domain.ScCourse;
import com.xfc.school.domain.ScTeacher;
import com.xfc.school.service.IScCourseService;
import com.xfc.school.service.IScTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


/**
 * 课程信息Controller
 * 
 * @author lcj
 * @date 2021-12-04
 */
@RestController
@Api(value = "class", tags = "课程管理接口")
@RequestMapping("/school/class")
public class ScCourseController extends BaseController
{
    @Autowired
    private IScCourseService scClassService;

    /**
     * 查询课程信息列表
     * @param className
     * @param type
     * @param state
     * @return
     */
    @ApiOperation("查询课程信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="className",value="班级名称",dataTypeClass = String.class),
            @ApiImplicitParam(name="type",value="类型",dataTypeClass = String.class),
    })
    @PreAuthorize("@ss.hasPermi('school:class:list')")
    @GetMapping("/list")
    public TableDataInfo list(String className,String type,String state)
    {
        QueryWrapper<ScCourse> queryWrapper=new QueryWrapper<>();
        //判断课程名称是否为空
        if (StringUtils.isNotEmpty(className)){
            queryWrapper.like("class_name",className);
        }
        //判断课程类型是否为空
        if (StringUtils.isNotEmpty(type)){
            queryWrapper.eq("type",type);
        }
        //判断课程状态是否为空
        if (StringUtils.isNotEmpty(state)){
            queryWrapper.eq("state",state);
        }
        startPage();
        List<ScCourse> list = scClassService.list(queryWrapper);

        return getDataTable(list);
    }

    /**
     * 导出课程信息列表
     */
    @PreAuthorize("@ss.hasPermi('school:class:export')")
    @Log(title = "课程信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ScCourse scClass)
    {
        List<ScCourse> list = scClassService.list();
        ExcelUtil<ScCourse> util = new ExcelUtil<ScCourse>(ScCourse.class);
        return util.exportExcel(list, "class");
    }

    /**
     * 获取课程信息详细信息
     */
    @ApiOperation("获取课程信息详细信息")
    @ApiImplicitParam(name="id",value="班级id",dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermi('school:class:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(scClassService.getById(id));
    }

    /**
     * 新增课程信息
     */
    @ApiOperation("查询课程信息列表")
    @PreAuthorize("@ss.hasPermi('school:class:add')")
    @Log(title = "课程信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ScCourse scClass) {
        //判断班级是否为空
        if (Objects.nonNull(scClass)){
            ScCourse queryScClass=scClassService.getOne(new QueryWrapper<ScCourse>().eq("class_name",scClass.getClassName()));
            if (Objects.nonNull(queryScClass)){
                return AjaxResult.error("课程名称已重复");
            }
        }
        return toAjax(scClassService.save(scClass));
    }

    /**
     * 修改课程信息
     */
    @ApiOperation("修改课程信息")
    @PreAuthorize("@ss.hasPermi('school:class:edit')")
    @Log(title = "课程信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ScCourse scClass)
    {
        return toAjax(scClassService.updateById(scClass));
    }

    /**
     * 删除课程信息
     */
    @ApiOperation("删除课程信息")
    @PreAuthorize("@ss.hasPermi('school:class:remove')")
    @Log(title = "课程信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids){
		List<String> list=new ArrayList<>();
    	if (ids!=null&&ids.length>0) {
            for (String id : ids) {
                list.add(id);
            }
		}
        return toAjax(scClassService.removeByIds(list));
    }

    /**
     * 查询所有课程
     */
    @ApiOperation("查询所有课程")
    @PreAuthorize("@ss.hasPermi('school:class:remove')")
    @Log(title = "查询所有课程", businessType = BusinessType.DELETE)
    @GetMapping("/getAllClass")
    public AjaxResult getAllClass(){
        QueryWrapper<ScCourse> queryWrapper=new QueryWrapper<>();
        queryWrapper.select("id","class_name");
        return AjaxResult.success(scClassService.list(queryWrapper));
    }

    /**
     *  根据课程名称模糊查询课程信息
     */
    @ApiOperation("根据课程名称模糊查询课程信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name="name",value="课程名称",dataTypeClass = String.class),
    })
    @GetMapping("/getCourseByName")
    public AjaxResult getCourseByName(String name)
    {
        QueryWrapper<ScCourse> queryWrapper=new QueryWrapper<>();
        if (StringUtils.isNotEmpty(name)){
            queryWrapper.like("class_name",name);
        }
        queryWrapper.eq("state",0);
        queryWrapper.select("id","class_name");
        List<ScCourse> list = scClassService.list(queryWrapper);
        return AjaxResult.success(list);
    }

}
